package com.toobigtofail.shapes
{
	import Box2D.Common.Math.b2Vec2;
	import Box2D.Dynamics.b2Body;
	import Box2D.Dynamics.b2Fixture;
	
	import fabulaexmachina.box2fp.Box2DEntity;
	import fabulaexmachina.box2fp.Box2DShapeBuilder;
	
	public class Octagon extends Box2DEntity implements IShape
	{
		private var _savedOptions:Object;
		
		public function Octagon(x:Number=0, y:Number=0, radius:Number=1, b2Type:int=0, group:int=0, category:int=0, collmask:int=0, friction:Number=0.3, density:Number=1, restitution:Number=0.3)
		{
			_savedOptions = {group: group, 
				             category: category, 
							 collmask: collmask, 
							 friction: friction, 
							 density: density, 
							 restitution: restitution,
						     b2Type: b2Type,
							 radius: radius};
			
			super(x, y, radius*2, radius*2, b2Type, group, category, collmask, friction, density, restitution);
		}
		
		override public function buildShapes(friction:Number, 
									density:Number, restitution:Number,
									group:int, category:int, collmask:int):void {
			buildShape(body);
		}
		
		public function buildShape(body:b2Body):void {
			var verts:Array = new Array;
			
			var xp:Number = Math.sin(Math.PI * (45/180)) * halfWidth;
			var yp:Number = Math.cos(Math.PI * (45/180)) * halfHeight;
			
			verts.push(new b2Vec2(0, -halfHeight));
			verts.push(new b2Vec2(xp, -yp));
			verts.push(new b2Vec2(halfWidth, 0));
			verts.push(new b2Vec2(xp, yp));
			verts.push(new b2Vec2(0, halfHeight));
			verts.push(new b2Vec2(-xp, yp));
			verts.push(new b2Vec2(-halfWidth, 0));
			verts.push(new b2Vec2(-xp, -yp));
			
			Box2DShapeBuilder.buildPolygon(body, verts, _savedOptions.friction, _savedOptions.density, _savedOptions.restitution, _savedOptions.group, _savedOptions.category, _savedOptions.collmask);
			body.SetType(_savedOptions.b2Type);
		}
		
		public function get b2Type():int { return _savedOptions.b2Type; }
		
		public function serialize():XML {
			var shape:XML = <shape type="Octagon"></shape>;
			shape.@x = x;
			shape.@y = y;
			shape.@b2Type = b2Type;
			shape.@radius = _savedOptions.radius;
			
			return shape;
		}
	}
}